package com.jiantong.web.open.app.user;

import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.model.DeclareStation;
import com.jiantong.user.service.DeclareStationReadService;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;


/**
 * Created by niufei on 2017/4/28.
 *
 */
@Slf4j
@OpenBean
public class DeclareStationApis {

    @RpcConsumer
    private DeclareStationReadService declareStationReadService;

    @OpenMethod(
            key = "find.local.declare.stations",
            httpMethods = RequestMethod.GET
    )
    public List<DeclareStation> findLocalDeclareStations(){
        LoginUser loginUser = UserUtil.getCurrentUser();
        if(loginUser == null){
            log.error("user is not login");
            throw new OPClientException("user is not login");
        }
        Response<List<DeclareStation>> response = declareStationReadService.findByProvinceIdAndCityId(loginUser);
        if(!response.isSuccess()){
            log.error("failed.to.find.declareStations, case = {}",response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }
}
